/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.controller.dashboard;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.SMSMailUtil;
import br.com.jr2tecnologia.model.CompanyPersistenceLocal;
import br.com.jr2tecnologia.model.LoginPersistenceLocal;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.entity.Login;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author ronildo
 */
@ManagedBean
@SessionScoped
public class CompanyDashboardMBean implements Serializable{
    
    private static final Logger logger = LoggerFactory.getLogger();

    @EJB
    private CompanyPersistenceLocal companyPersistence;
    @EJB
    private LoginPersistenceLocal loginPersistence;

    private Company selectedCompany;
    private List<Company> listCompany;
    private List<Login> listLogin;

    /** Creates a new instance of CompanyDashboardMBean */
    public CompanyDashboardMBean() {
    }
    
    @PostConstruct
    public void initialize(){
        selectedCompany = new Company();
        searchCompany();
        searchLogins();
    }

    public List<Company> getListCompany() {
        return listCompany;
    }

    public List<Login> getListLogin() {
        return listLogin;
    }

    public Company getSelectedCompany() {
        return selectedCompany;
    }

    public void setSelectedCompany(Company selectedCompany) {
        this.selectedCompany = selectedCompany;
    }

    public void searchCompany(){
        listCompany = companyPersistence.listAll();
    }

    public void searchLogins(){
        listLogin = loginPersistence.list(new Login());
    }

    public void updateCompany(){
        companyPersistence.update(selectedCompany);
        SMSMailUtil.putFacesMessage("Sucesso", "Empresa "+selectedCompany.getName()+" atualizada com sucesso");
        searchCompany();
    }

    public void removeCompany(){
        companyPersistence.removeCascade(selectedCompany);
        SMSMailUtil.putFacesMessage("Sucesso", "Empresa "+selectedCompany.getName()+" removida com sucesso");
        searchCompany();
    }

}
